package org.zhq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zhq.enums.YesOrNo;
import org.zhq.mapper.UserAddressMapper;
import org.zhq.pojo.UserAddress;
import org.zhq.pojo.bo.AddressBO;
import org.zhq.service.AddressService;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    private final UserAddressMapper userAddressMapper;

    @Autowired
    public AddressServiceImpl(UserAddressMapper userAddressMapper) {
        this.userAddressMapper = userAddressMapper;
    }

    /**
     * 根据用户id 查询用户的收货地址列表
     *
     * @param userId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserAddress> queryAll(String userId) {
        LambdaQueryWrapper<UserAddress> condition = Wrappers.<UserAddress>lambdaQuery().eq(UserAddress::getUserId, userId);
        return userAddressMapper.selectList(condition);
    }

    /**
     * 用户新增地址
     *
     * @param addressBO
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addNewUserAddress(AddressBO addressBO) {
        //0.判断当前用户是否存在地址

        LambdaQueryWrapper<UserAddress> condition = Wrappers.<UserAddress>lambdaQuery().eq(UserAddress::getUserId, addressBO.getUserId());
        Integer count = userAddressMapper.selectCount(condition);
        Integer isDefault = count == 0 ? 1 : 0;
        //1.保存地址到数据库
        UserAddress newAddress = new UserAddress();
        BeanUtil.copyProperties(addressBO,newAddress);
        newAddress.setIsDefault(isDefault);
        newAddress.setCreatedTime(new Date());
        newAddress.setUpdatedTime(new Date());
        userAddressMapper.insert(newAddress);
    }

    /**
     * 用户修改地址
     *
     * @param addressBO
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserAddress(AddressBO addressBO) {
        String addressId = addressBO.getAddressId();
        UserAddress pendingAddress = new UserAddress();
        BeanUtil.copyProperties(addressBO,pendingAddress);
        pendingAddress.setId(addressId);
        pendingAddress.setUpdatedTime(new Date());
        userAddressMapper.updateById(pendingAddress);
    }

    /**
     * 用户删除地址
     *
     * @param userId
     * @param addressId
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteUserAddress(String userId, String addressId) {
        LambdaQueryWrapper<UserAddress> condition = Wrappers.<UserAddress>lambdaQuery()
                .eq(UserAddress::getId, addressId)
                .eq(UserAddress::getUserId, userId);
        userAddressMapper.delete(condition);
    }

    /**
     * 用户设置默认地址
     *
     * @param userId
     * @param addressId
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserAddressToBeDefault(String userId, String addressId) {
        //0.查询默认地址 设置不默认
        UserAddress ua = new UserAddress();
        ua.setIsDefault(YesOrNo.NO.type);
        LambdaQueryWrapper<UserAddress> condition = Wrappers.<UserAddress>lambdaQuery()
                .eq(UserAddress::getUserId,userId)
                .eq(UserAddress::getIsDefault, YesOrNo.YES.type);
        userAddressMapper.update(ua,condition);
        //1.根据地址id修改为默认地址
        UserAddress ua2 = new UserAddress();
        ua2.setIsDefault(YesOrNo.YES.type);
        LambdaQueryWrapper<UserAddress> condition2 = Wrappers.<UserAddress>lambdaQuery()
                .eq(UserAddress::getUserId, userId)
                .eq(UserAddress::getId, addressId);
        userAddressMapper.update(ua2,condition2);
    }

    /**
     * 根据用户id 地址id 查询用户地址
     *
     * @param userId
     * @param addressId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UserAddress queryUserAddress(String userId, String addressId) {
        return userAddressMapper.selectOne(Wrappers.<UserAddress>lambdaQuery()
                .eq(UserAddress::getUserId,userId)
                .eq(UserAddress::getId,addressId));
    }

}
